package br.com.hstar.client.util;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;


public class HstarDialogBox extends DialogBox {

	public static final int OK_OPTION      = 10;
	public static final int YES_OPTION     = 11;
	public static final int NO_OPTION      = 12;
	
	// Attributes ----------------------------------------------------
	
	protected Button yesButton;
	protected Button noButton;
	protected Button okButton;	
	protected DialogPopupListener listener;
	
	// Static --------------------------------------------------------

	
	
	// Constructors --------------------------------------------------

	/**
	 * Default constructor
	 */
	public HstarDialogBox(String title, String message, DialogPopupListener listener) {
		this.listener = listener;		
		// ajusta as propriedades do DialogPopup
	    setText(title);
	    
	    // cria o painel
	    cratePanel(message);
	    
	    // exibe o DialogPopup
	    show();
	    
	    // Centraliza p DialogPopup
	    center();
	}
	
	/**
	 * Default constructor
	 */
	public HstarDialogBox(String title, String message) {
		this(title, message, null);
	}

	
	private void cratePanel(String message){
		
	    // Cria o DockPanel que irÃ¡ conter o texto 'sobre' e o botÃ£o 'OK'.
	    DockPanel dockPanel = new DockPanel();
	    dockPanel.setSpacing(20);



	    DialogPopupClickListner buttonListener = new DialogPopupClickListner();
	    

    	// Se for qualquer outro tipo, menos o de Wait, adiciona os botÃµes
    	
	    // Cria o botÃ£o 'OK', e adiciona um escutador para esconder o
		// dialogpopup
	    // Alinha o botÃ£o centralmente ao painal 'buttonPanel'
	    okButton = new Button("Ok");
	    okButton.addClickListener(buttonListener);

	    // Adiciona o painel que contem o botÃ£o 'OK' ao DockPanel e alinha
		// centralmente
	    dockPanel.add(okButton, DockPanel.SOUTH);
	    dockPanel.setCellHorizontalAlignment(okButton, DockPanel.ALIGN_CENTER);
    
	    
	    // Cria o texto 'sobre'.
	    HTML text = new HTML(message);
	    dockPanel.add(text, DockPanel.CENTER);
	    dockPanel.setCellHorizontalAlignment(text, DockPanel.ALIGN_LEFT);
	    
	    // Adiciona o DockPanel ao dialog.
	    setWidget(dockPanel);
	}
	
	// Inner classes -------------------------------------------------
	
	/**
	 * Escutador para o botÃ£o OK, utilizado para fechar o DialogPopup
	 * 
	 * @author tco
	 */
	private class DialogPopupClickListner implements ClickListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
		 */
		public void onClick(Widget sender) {
			HstarDialogBox.this.hide();
			if (listener != null) {
				if (sender.equals(okButton)) {
					listener.onOptionSelected(OK_OPTION);
				} else if (sender.equals(yesButton)) {
					listener.onOptionSelected(YES_OPTION);
				} else if (sender.equals(noButton)) {
					listener.onOptionSelected(NO_OPTION);
				}
			}
		}
	}
}





